<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/5/14
 * Time: 9:04
 */

namespace app\admin\controller;


use app\admin\model\CityRange;
use app\admin\model\DriverType;
use app\admin\model\GoodsList;
use think\facade\Env;

class Goods extends Base
{

    public function goodsImg(){
        $file =request()->file('file');
        $ret = $this->upload($file);
        if (!$ret){
            return json(['code'=>0,'msg'=>'上传失败']);
        }
        return json(['code'=>1,'url'=>$ret]);
    }
    public function goodsImgDel(){
        $url = "../public".input('post.imgUrl');
        if (file_exists($url)){
            if (!unlink($url)){
                return json(['code'=>0,'msg'=>'删除失败']);
            }
        }
        return json(['code'=>1,'msg'=>'删除成功']);
    }


    public function goodsList(){
        return $this->fetch();
    }

    public function goodsData($page,$limit){
        $where = input('get.where')?[['good_name','like','%'.trim(input('get.where')).'%']]:'';
        $data = GoodsList::where($where)->order('sort asc')->page($page,$limit)->select()->toArray();
        $count = GoodsList::where($where)->count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }

    public function goodsAdd(){
        if ($this->request->isPost()){
            $post = input('post.');
            $validate = $this->validate($post,'app\admin\validate\Goods.add');
            if (true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $data = [
                'good_name'=>$post['good_name'],
                'image'=>$post['image'],
                'images'=>implode(',',$post['images']),
                'range_id'=>implode(',',$post['range_id']),
                'driver_id'=>$post['driver_id'],
                'content'=>$post['content'],
                'note'=>$post['note'],
                'price'=>$post['price'],
                'old_price'=>$post['old_price'],
                'is_home'=>$post['is_home'],
                'status'=>$post['status'],
                'sort'=>$post['sort'],
                'create_time'=>time(),
                'address'=>$post['address'],
                'lng'=>$post['lng'],
                'lat'=>$post['lat'],
            ];
            $ret = GoodsList::create($data);
            if (!$ret){
                return json(['code'=>0,'msg'=>Env::get('msg.add_error')]);
            }
            return json(['code'=>1,'msg'=>Env::get('msg.add_success')]);
        }
        $range = CityRange::order('sort asc')->select()->toArray();
        $driver = DriverType::order('sort asc')->select()->toArray();
        $this->assign('range',$range);
        $this->assign('driver',$driver);
        return $this->fetch();
    }


    public function goodsEdit($id){
        if ($this->request->isPost()){
            $post = input('post.');
            $validate = $this->validate($post,'app\admin\validate\Goods.edit');
            if (true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $data = [
                'good_name'=>$post['good_name'],
                'image'=>$post['image'],
                'images'=>implode(',',$post['images']),
                'range_id'=>implode(',',$post['range_id']),
                'driver_id'=>$post['driver_id'],
                'content'=>$post['content'],
                'note'=>$post['note'],
                'price'=>$post['price'],
                'old_price'=>$post['old_price'],
                'is_home'=>$post['is_home'],
                'status'=>$post['status'],
                'sort'=>$post['sort'],
                'address'=>$post['address'],
                'lng'=>$post['lng'],
                'lat'=>$post['lat']
            ];
            $ret = GoodsList::update($data,['id'=>$post['id']]);
            if (!$ret){
                return json(['code'=>0,'msg'=>Env::get('msg.add_error')]);
            }
            return json(['code'=>1,'msg'=>Env::get('msg.add_success')]);
        }
        $range = CityRange::order('sort asc')->select()->toArray();
        $driver = DriverType::order('sort asc')->select()->toArray();
        $data  = GoodsList::where('id',$id)->find();
        $data['images'] = explode(',',$data['images']);
        $data['range_id'] = explode(',',$data['range_id']);
        $data['ranges'] = CityRange::where('id','in',$data['range_id'])->select()->toArray();
        $this->assign('range',$range);
        $this->assign('driver',$driver);
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function goodsStatus(){
        $post = input('post.');
        GoodsList::update([$post['name']=>$post['value']],['id'=>$post['id']]);
        return 1;
    }

    public function goodsDel(){
        $id = input('post.id');
        $ret = GoodsList::destroy($id);
        if (!$ret){
            return json(['code'=>0,'msg'=>Env::get('msg.del_error')]);
        }
        return json(['code'=>1,'msg'=>Env::get('msg.del_success')]);
    }



}